/**
 *   sci4j - Java library for scientific computation.
 *
 *   Copyright (C) 2010 Inaki Ortiz de Landaluce
 *
 *   This program is free software: you can redistribute it and/or modify
 *   it under the terms of the GNU General Public License as published by
 *   the Free Software Foundation, either version 3 of the License, or
 *   (at your option) any later version.
 *
 *   This program is distributed in the hope that it will be useful,
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *   GNU General Public License for more details.
 *
 *   You should have received a copy of the GNU General Public License
 *   along with this program.  If not, see <http://www.gnu.org/licenses/>.
 *
 */
package org.sci4j.imaging.plugins.dicom;

import java.io.IOException;
import java.util.Locale;
import javax.imageio.ImageReader;
import javax.imageio.spi.ImageReaderSpi;
import javax.imageio.stream.ImageInputStream;

public class DICOMImageReaderSpi extends ImageReaderSpi {

	public static final String VENDOR_NAME = "org.sci4j.imaging";
	public static final String VERSION = "0.1";
	public static final String READER_CLASS_NAME = "org.sci4j.imaging.plugins.dicom.DICOMImageReader";	
	public static final String[] NAMES = {"dicom", "DICOM"};
	public static final String[] SUFFIXES = {"dcm", "dic", "dicm", "dicom"};
	public static final String[] MIME_TYPES = {"application/dicom"};
	//accept image input streams only
	@SuppressWarnings("unchecked")
	public static final Class[] INPUT_TYPES = STANDARD_INPUT_TYPE;  
	public static final String[] WRITER_SPI_NAMES = {"org.sci4j.imaging.plugins.dicom.DICOMImageWriterSpi"};
	
	public static final String NATIVE_STREAM_METADATA_FORMAT_NAME = null;
	public static final String NATIVE_STREAM_METADATA_FORMAT_CLASS_NAME = null;
	public static final String NATIVE_IMAGE_METADATA_FORMAT_NAME = null;
	public static final String NATIVE_IMAGE_METADATA_FORMAT_CLASS_NAME = null;	
	public static final String[] EXTRA_STREAM_METADATA_FORMAT_NAMES = {""};
	public static final String[] EXTRA_STREAM_METADATA_FORMAT_CLASS_NAMES = {""};
	public static final String[] EXTRA_IMAGE_METADATA_FORMAT_NAMES = {""};
	public static final String[] EXTRA_IMAGE_METADATA_FORMAT_CLASS_NAMES = {""};
		
	public static final boolean SUPPORTS_STANDARD_STREAM_METADATA_FORMAT = false;
	public static final boolean SUPPORTS_STANDARD_IMAGE_METADATA_FORMAT = false;
	
	public DICOMImageReaderSpi() {
		super(VENDOR_NAME, VERSION, NAMES, SUFFIXES, MIME_TYPES, READER_CLASS_NAME,
				INPUT_TYPES, WRITER_SPI_NAMES, SUPPORTS_STANDARD_STREAM_METADATA_FORMAT,
				NATIVE_STREAM_METADATA_FORMAT_NAME, NATIVE_STREAM_METADATA_FORMAT_CLASS_NAME,
				EXTRA_STREAM_METADATA_FORMAT_NAMES, EXTRA_STREAM_METADATA_FORMAT_CLASS_NAMES,
				SUPPORTS_STANDARD_IMAGE_METADATA_FORMAT, NATIVE_IMAGE_METADATA_FORMAT_NAME,
				NATIVE_IMAGE_METADATA_FORMAT_CLASS_NAME, EXTRA_IMAGE_METADATA_FORMAT_NAMES,
				EXTRA_IMAGE_METADATA_FORMAT_CLASS_NAMES);
	}

	/* 
	 * @see javax.imageio.spi.ImageReaderSpi#canDecodeInput(java.lang.Object)
	 */
	@Override
	public boolean canDecodeInput(Object source) throws IOException {
		if(!(source instanceof ImageInputStream)) { // STANDARD_INPUT_TYPE supported only
			return false; 
		}		
		// PS3.10-2009, Section 7.1
		// The DICOM File Meta information consists of a fixed 128-byte 
		// field, followed by a 4-byte DICOM prefix containing
		// the character string "DICOM" plus a set of DICOM Meta Elements.
		// Read first 132 bytes and look for the proper char sequence.
		ImageInputStream stream = (ImageInputStream)source;
		byte[] b = new byte[132];
		stream.mark();
		try {			
			stream.readFully(b);			
		} catch(IOException e) {
			return false;
		} finally{
			stream.reset();
		}		
		if(b[128] == (byte)'D' && b[129] == (byte)'I'
		   && b[130] == (byte)'C' && b[131] == (byte)'M') {
			return true;
		}
		return false;
	}

	/* 
	 * @see javax.imageio.spi.ImageReaderSpi#createReaderInstance(java.lang.Object)
	 */
	@Override
	public ImageReader createReaderInstance(Object extension)
			throws IOException {
		return new DICOMImageReader(this);
	}

	/* 
	 * @see javax.imageio.spi.IIOServiceProvider#getDescription(java.util.Locale)
	 */
	@Override
	public String getDescription(Locale locale) {
		return "sci4j-imaging DICOM Image Reader";
	}
	
}